/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import models.Agenda_Announcement;
import models.Course;
import models.User;
import org.hibernate.Session;
import service.AnnouncementDao;
import service.CourseDao;
import service.HibernateUtil;
import service.UserDao;

/**
 *
 * @author Serife
 */
public class AgendaControllerStudent extends HttpServlet {

    private HibernateUtil HU = new HibernateUtil();
    private Session sessie = HU.openSession();
    private CourseDao cd = new CourseDao(sessie);
    private AnnouncementDao adao = new AnnouncementDao(sessie);

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        HttpSession session = request.getSession(true);
        //get the course id and display announcements
        if (request.getParameter("sc") != null) { //SC SELECTED COURSE
            Long item = Long.parseLong(request.getParameter("sc"));
            Course course = cd.get(item);
            Set<Agenda_Announcement> announcements = course.getAnnouncements();

            request.setAttribute("announcements", announcements);
            request.setAttribute("selectedCourse", course);

            if (request.getParameter("sa") != null) { //SA SELECTED Announcement
                try{
                    Agenda_Announcement announcement = adao.get(Integer.parseInt(request.getParameter("sa")));
                               request.setAttribute("selectedAnnouncement", announcement);
                }catch(Exception e){
                    System.out.println(e);
            }
            }

            forward(request, response);

        } else {
            response.sendRedirect("/");
        }
    }

    protected void forward(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String address = "/studentannouncements.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }
}
